<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016-11-14
 * Time: 下午 2:03
 */
defined('ByShopWWI') or exit('Access Invalid!');
class goodsControl extends MobileControl{

    /**
     * 根据类别id查询商品列表
     */
    public function indexOp(){
        $model_goods = Model('goods');
        $gc_id = intval($_GET['gc_id']);
        $gc_name = $_GET['gc_name'];

        $condition = array();
        $condition['is_book'] = 0;
        $condition['gc_id'] = $gc_id;
        $order = $this->_goods_list_order($_GET['key'], $_GET['order']);
        $goods_list = $model_goods->getGoodsList($condition, '*','', $order,0,10);

        Tpl::output('goods_list_category',$goods_list);
        Tpl::output('gc_name',$gc_name);
        Tpl::output('totalPage',$model_goods->gettotalpage());
        Tpl::showpage('product_list');
    }

    public function indexPageOp(){
        $_GET['curpage'] = $_GET['curpage']+1;//当前页

        $model_goods = Model('goods');
        $gc_id = intval($_GET['gc_id']);

        $condition = array();
        $condition['is_book'] = 0;
        $condition['gc_id'] = $gc_id;

        $order = $this->_goods_list_order($_GET['key'], $_GET['order']);
        $goods_list = $model_goods->getGoodsList($condition, '*','', $order,0,10);

        foreach($goods_list as $k => $v){
            $v['goods_image_url'] =  cthumb($v['goods_image'], 265, $v['store_id']);
            $arr[] = $v;
        }
        $goods_list = $arr;

        output_data(array('goods_list' => $goods_list,'curpage' => $_GET['curpage']));
    }

    /**
     * 商品详情页
     */
    public function goods_detailOp(){
        $goods_id = intval($_GET['goods_id']);
        $favorites_model = Model('favorites');

        // 商品详细信息
        $model_goods = Model('goods');
        $goods_detail = $model_goods->getGoodsDetail($goods_id);

        if (empty($goods_detail)) {
            output_error('商品不存在');
        }

        //大家都在看
        $top_goods_list = $model_goods->getGoodsOnlineList(array('store_id' => 1,), '*', 0, 'rand()', 4);

        //商品详细信息处理
        $goods_detail = $this->_goods_detail_extend($goods_detail);
        //其他规格商品循环
//        if($goods_detail['spec_list']){
//            foreach($goods_detail['spec_list'] as $k=>$v){
//                $goods_spec[$v]=$model_goods->getGoodsDetail($v);
//            }
//        }
//        Tpl::output('goods_spec', $goods_spec);
//        var_dump($goods_spec[100119]['goods_info']['goods_name']);exit;
        //连接数组 规格名与商品id
        if($goods_detail['spec_list'] && $goods_detail['goods_info']['spec_value']){
            $goods_detail['spec_list'] =array_combine($goods_detail['spec_list'],current($goods_detail['goods_info']['spec_value']));
        }
        // 如果已登录 判断该商品是否已被收藏
        if($_SESSION['member_id']) {
            $goods_detail['cart_count'] = Model('cart')->countCartByMemberId($_SESSION['member_id']);
        }

        if($goods_detail){
            //$model_goods_browse = Model('goods_browse')->addViewedGoods($goods_id,$_SESSION['member_id']); //加入浏览历史数据库
            Tpl::output('goods_detail',$goods_detail);
        }

        $is_fa = $favorites_model->getOneFavorites(array('fav_id'=>"$goods_id",'fav_type'=>'goods','member_id'=>"{$_SESSION['member_id']}"));

        if($is_fa){
            $favo= true;
            Tpl::output('favo',$favo);
        }else{
            $favo= false;
            Tpl::output('favo',$favo);
        }

        //评论数量
        $goods_evaluate_info = Model('evaluate_goods')->getEvaluateGoodsInfoByGoodsID($goods_id);
        Tpl::output('goods_evaluate_info', $goods_evaluate_info);

        $goodsevallist = Model('evaluate_goods')->getEvaluateGoodsList(array('geval_goodsid' => $goods_id,), 1);
        Tpl::output('goodsevallist',$goodsevallist);
        Tpl::output('top_goods_list',$top_goods_list);
        Tpl::showpage('goods_detail');
    }

    //加入收藏夹   hyy 2017.3.15
    public function favoritegoodsOp(){
        $fav_id = intval($_POST['goods_id']);

        $favorites_model = Model('favorites');
        //判断是否已经收藏
        $favorites_info = $favorites_model->getOneFavorites(array('fav_id'=>"$fav_id",'fav_type'=>'goods','member_id'=>"{$_SESSION['member_id']}"));
        if(!empty($favorites_info)){
             exit();
        }else{

            //判断商品是否为当前会员所有
            $goods_model = Model('goods');
            $goods_info = $goods_model->getGoodsInfoByID($fav_id, 'store_id');

            //添加收藏
            $insert_arr = array();
            $insert_arr['member_id'] = $_SESSION['member_id'];
            $insert_arr['member_name'] = $_SESSION['member_name'];
            $insert_arr['fav_id'] = $fav_id;
            $insert_arr['fav_type'] = 'goods';
            $insert_arr['fav_time'] = time();
            $result = $favorites_model->addFavorites($insert_arr);
            if ($result){
                //增加收藏数量
                $goods_model->editGoodsById(array('goods_collect' => array('exp', 'goods_collect + 1')), $fav_id);
            }

        }

    }


    /**
     * 评价列表
     */
    public function comment_listOp(){
        $goods_id = intval($_GET ['goods_id']);

        //评论数量
        $goods_evaluate_info = Model('evaluate_goods')->getEvaluateGoodsInfoByGoodsID($goods_id);
        Tpl::output('goods_evaluate_info', $goods_evaluate_info);

        $goodsevallist = Model('evaluate_goods')->getEvaluateGoodsList(array('geval_goodsid' => $goods_id,));
        Tpl::output('goodsevallist',$goodsevallist);

        $pic_num = 0;
        foreach($goodsevallist as $k => $v){
            if(!empty($v['geval_image'])) {
                $pic_arr[] = $v;
                $pic_num += 1;
            }
        }

        Tpl::output('pic_arr',$pic_arr);
        Tpl::output('pic_num',$pic_num);
        Tpl::showpage('comment_list');
    }

    /**
     * 商品详细信息处理
     */
    private function _goods_detail_extend($goods_detail) {
        //整理商品规格
        unset($goods_detail['spec_list']);
        $goods_detail['spec_list'] = $goods_detail['spec_list_mobile'];
        unset($goods_detail['spec_list_mobile']);

        //整理商品图片
       // unset($goods_detail['goods_image']);
        //$goods_detail['goods_image'] = implode(',', $goods_detail['goods_image_mobile']);
        //unset($goods_detail['goods_image_mobile']);

        //商品链接
        $goods_detail['goods_info']['goods_url'] = urlShop('goods', 'index', array('goods_id' => $goods_detail['goods_info']['goods_id']));

        //整理数据
//        unset($goods_detail['goods_info']['goods_commonid']);
        unset($goods_detail['goods_info']['gc_id']);
        unset($goods_detail['goods_info']['gc_name']);
        unset($goods_detail['goods_info']['store_id']);
        unset($goods_detail['goods_info']['store_name']);
        unset($goods_detail['goods_info']['brand_id']);
        unset($goods_detail['goods_info']['brand_name']);
        unset($goods_detail['goods_info']['type_id']);
        unset($goods_detail['goods_info']['goods_image']);
//        unset($goods_detail['goods_info']['goods_body']);
        unset($goods_detail['goods_info']['goods_state']);
        unset($goods_detail['goods_info']['goods_stateremark']);
        unset($goods_detail['goods_info']['goods_verify']);
        unset($goods_detail['goods_info']['goods_verifyremark']);
        unset($goods_detail['goods_info']['goods_lock']);
        unset($goods_detail['goods_info']['goods_addtime']);
        unset($goods_detail['goods_info']['goods_edittime']);
        unset($goods_detail['goods_info']['goods_selltime']);
        unset($goods_detail['goods_info']['goods_show']);
        unset($goods_detail['goods_info']['goods_commend']);
        unset($goods_detail['goods_info']['explain']);
        unset($goods_detail['goods_info']['buynow_text']);
        unset($goods_detail['groupbuy_info']);
        unset($goods_detail['xianshi_info']);

        return $goods_detail;
    }

    /**
     * 商品列表排序方式
     */
    private function _goods_list_order($key, $order) {

        $key = empty($key) ? 'rate' : $key;
        $sequence = empty($order) ? 'desc' : $order;

        switch ($key) {
            //销量
            case 'sales' :
                $result = 'goods_salenum' . ' ' . $sequence;
                break;
            //浏览量
            case 'click' :
                $result = 'goods_click' . ' ' . $sequence;
                break;
            //价格
            case 'price' :
                $result = 'goods_price' . ' ' . $sequence;
                break;
            //收益率
            case 'rate' :
                $result = 'year_rate' . ' ' . $sequence;
                break;
        }
        return $result;
    }

    //ajax商品信息
    public function ajaxinfoOp(){
        $goods_id = $_GET['goods_id'];
        $model_goods = Model('goods');
        $goods_detail = $model_goods->getGoodsDetail($goods_id);
        echo json_encode($goods_detail);
    }
}